package com.imti.common;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Result<T> {

    private long code;//编码 200/400
    private String message;//成功/失败
    private T data;//数据

    protected Result() {
    }

    protected Result(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Result<T> fail(){
        return new Result(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), null);
    }

    public static <T> Result<T> fail(IErrorCode errorCode){
        return new Result(errorCode.getCode(), errorCode.getMessage(), null);
    }

    public static <T> Result<T> fail(IErrorCode errorCode, String message){
        return new Result(errorCode.getCode(), message, null);
    }

    public static <T> Result<T> fail(String message){
        return new Result(ResultCode.FAILED.getCode(), message, null);
    }

    public static <T> Result<T> success(){
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
    }

    public static <T> Result<T> success(T data){
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> success(String message, T data){
        return new Result(ResultCode.SUCCESS.getCode(), message, data);
    }
}
